package suanfa.lianxi;
import java.util.Scanner;
 class Shitang {
        private node front,rear;//队头队尾
        private int size;//队内元素数量

        public Shitang(){
            this.front=null;
            this.rear=null;
            size=0;
        }

    public boolean isEmpty(){
        return this.front==null&&rear==null;
    }
    public void enqueue(String id){//入队方法
            node newnode= new node(id);
            if(isEmpty()){
                front=newnode;
            }else{
                rear.next=newnode;

            }
            rear=newnode;
            size++;
    }

    public String dequeue(){
            if(isEmpty()){
                throw new RuntimeException("队列已空");
            }
            String oid=front.id;
            front=front.next;
            if(front==null){
                rear=null;
            }
            size--;
            return oid;

    }

    public String peek(){
            if(isEmpty()){
                throw new RuntimeException("队列已空");
            }
            return front.id;

    }


     private static class node{
          String id;
          node next;
         node(String id){
             this.id=id;
             this.next=null;
         }
     }


     public static void main(String[] args) {
         Shitang link=new Shitang();
         Scanner sc=new Scanner(System.in);
         int cout=0;
         System.out.println("欢迎使用食堂取餐系统");
         while(true){
             System.out.println("请输入序号操作系统");
             System.out.println("1--排队取号");
             System.out.println("2--取餐");
             System.out.println("3--查看队列");
             System.out.println("4--退出系统");

             int p=sc.nextInt();
             if(p>4){
                 System.out.println("请重新输入序号");
                 continue;
             }
             switch (p){
                 case 1:
                     String orderid=String.format("%03d",++cout);
                     link.enqueue(orderid);
                     System.out.println("当前单号已进入队列: "+orderid);
                     break;

                 case 2:
                     String o=link.dequeue();
                     System.out.println(o+" 已出餐");
                     if(link.isEmpty()){
                         System.out.println("已全部出餐完毕");
                     }
                     break;

                 case 3:
                     System.out.println("当前排队的单号数为: "+link.size);
                     System.out.println("下一个待出餐的单号为: "+link.peek());
                     break;

                 case 4:
                     System.out.println("已退出系统");
                     sc.close();
                     System.exit(0);
             }



         }
     }


}
